#include "SDL_GraphicsEngine.h"

SDL_GraphicsEngine *SDL_GraphicsEngine::pInstance = 0;
SDL_Surface *SDL_GraphicsEngine::screen = 0;
SDL_GraphicsEngine::SDL_GraphicsEngine(void)
{
	
}


SDL_GraphicsEngine::~SDL_GraphicsEngine(void)
{
	clean_up();
}
bool SDL_GraphicsEngine::init(char* wincaption)
{
	screen = NULL;

    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;
    }

    //Initialize SDL_ttf
    if( TTF_Init() == -1 )
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( wincaption, NULL );

    //If everything initialized fine
    return true;
}
void SDL_GraphicsEngine::apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip )
{
	//Holds offsets
	SDL_Rect offset;

	//Get offsets
	offset.x = x;
	offset.y = y;

	//Blit
	SDL_BlitSurface( source, clip, destination, &offset );
}
SDL_Surface *SDL_GraphicsEngine::load_image( std::string filename )
	{
		//The image that's loaded
		SDL_Surface* loadedImage = NULL;

		//The optimized surface that will be used
		SDL_Surface* optimizedImage = NULL;

		//Load the image
		loadedImage = IMG_Load( filename.c_str() );

		//If the image loaded
		if( loadedImage != NULL )
		{
			//Create an optimized surface
			optimizedImage = SDL_DisplayFormat( loadedImage );

			//Free the old surface
			SDL_FreeSurface( loadedImage );

			//If the surface was optimized
			if( optimizedImage != NULL )
			{
				//Color key surface
				SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
			}
		}

		//Return the optimized surface
		return optimizedImage;
	}

////////////////////////////////////////////////////////////////
//
//Function		:   GetInstance
//
//Purpose		:	Makes the class the first time it is called
//					and then returns the address of the class
///////////////////////////////////////////////////////////////
SDL_GraphicsEngine *SDL_GraphicsEngine::GetInstance(void)
{
	//	Check to see if one hasn't been made yet
	if(pInstance == 0)
		pInstance = new SDL_GraphicsEngine();

	//	Return the address of the instance.
	return pInstance;
}
////////////////////////////////////////////////////
//
//Function		:   DeleteInstancce
//
//
//Purpose		:	Delete the instance of the class
////////////////////////////////////////////////////
void SDL_GraphicsEngine::DeleteInstancce(void)
{

	if (pInstance)
	{
		delete pInstance;
		pInstance = 0;
	}
}
void SDL_GraphicsEngine::clean_up()
{
    ////Free the surfaces
    //SDL_FreeSurface( background );
    //SDL_FreeSurface( up );
    //SDL_FreeSurface( down );
    //SDL_FreeSurface( left );
    //SDL_FreeSurface( right );

    ////Close the font
    //TTF_CloseFont( font );

    //Quit SDL_ttf
    TTF_Quit();

    //Quit SDL
    SDL_Quit();
}